<?php

declare(strict_types=1);

namespace App\AdminApi\Module\Admin\Api;

use App\AdminApi\AdminApiBasicsController;
use App\AdminApi\Module\Admin\Search\LogSearch;
use App\AdminApi\Module\Admin\Service\LogService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\{Controller, GetMapping};
use Psr\Http\Message\ResponseInterface;
use Zms\Guard\Auth;
use Zms\HyperfResponse\Response;

#[
    Controller(prefix: "/admin-api/admin-log", server: self::SERVER),
    Auth(guard: self::GUARD, label: ['后台管理', '操作记录'], app: self::APP)
]
class LogController extends AdminApiBasicsController
{

    #[Inject]
    protected LogService $service;


    #[
        GetMapping('list'),
        Auth(label: ['操作日志'], sign: "admin.log.list", logger: false, view: '/admin/log/index')
    ]
    public function list(): ResponseInterface
    {
        $search = new LogSearch([
            'where' => [
                ...$this->request->inputs(['name', 'ip', 'url', 'time', 'os']),
                'user_id' => $this->request->input('user_id') ?: $this->dataAuth()
            ],
            'size' => $this->pageSize(),
            'page' => (int)$this->request->input('page', 1),
            'order' => $this->orderBy('id DESC'),
            'column' => ['id', 'name', 'status', 'msg', 'method', 'url', 'ip', 'ascription', 'browser', 'os', 'guard', 'user_id', 'time', 'param', 'callback']
        ]);
        return Response::Api()->success(data: $search->search());
    }


    #[
        GetMapping('export'),
        Auth(label: ['导出日志'], sign: "admin.log.export")
    ]
    public function export(): ResponseInterface
    {
        $search = new LogSearch([
            'where' => [
                ...$this->request->inputs(['name', 'ip', 'url', 'time', 'os']),
                'user_id' => $this->dataAuth()
            ],
            'order' => $this->orderBy('id DESC'),
            'column' => ['name', 'method', 'url', 'ip', 'guard', 'app', 'time', 'status', 'browser'],
        ]);
        return Response::Api()->success(data: $search->export(date("操作日志YmdHis")));
    }
}